package com.taxshare.tools.util;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.Months;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;

/**
 * 日期类
 *
 * @author chenxd
 * @date 2019/04/29
 */
@SuppressWarnings("all")
@Slf4j
public class DateTimeUtil {

	/**
	 * yyyyMMddHHmmss
	 */
	public static final String DATEFORMATYMDHMS = "yyyyMMddHHmmss";
	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static final String DATE_FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";

	/**
	 * yyyy-MM-dd
	 */
	public static final String DATE_FORMAT_YMD = "yyyy-MM-dd";

	/**
	 * yyyyMM
	 */
	public static final String DATE_FORMAT_YM = "yyyyMM";

	/**
	 * yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
	 */
	public static final String DATE_FORMAT_YMD_Z = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

	/**
	 * 00:00:00
	 */
	public static final String DATE_START_TIME = "00:00:00";

	/**
	 * 23:59:59
	 */
	public static final String DATE_END_TIME = "23:59:59";

	/**
	 * mongo日期查询isoDate
	 */
	public static Date dateToISODate(String dateStr) {
		// T代表后面跟着时间，Z代表UTC统一时间
		SimpleDateFormat simple = new SimpleDateFormat(DATE_FORMAT_YMD);
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YMD_Z);
		format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
		try {
			return format.parse(format.format(simple.parse(dateStr)));
		} catch (ParseException e) {
			log.error("时间转换异常", e);
		}
		return null;
	}

	/**
	 * mongo日期查询isoDate
	 */
	public static Date dateToISODate(String dateStr, String suffix) {
		// T代表后面跟着时间，Z代表UTC统一时间
		SimpleDateFormat simple = new SimpleDateFormat(DATE_FORMAT_YMDHMS);
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YMD_Z);
		format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
		try {
			return format.parse(format.format(simple.parse(dateStr + " " + suffix)));
		} catch (ParseException e) {
			log.error("时间转换异常", e);
		}
		return null;
	}

	/**
	 * 计算日期 月份相差
	 * 
	 * @param startTime
	 *            开始日期
	 * @param endTime
	 *            结束日期
	 */
	public static int computedMonth(String startTime, String endTime) {
		// 计算月份
		DateTimeFormatter formatter = DateTimeFormat.forPattern(DATE_FORMAT_YMD);
		DateTime start = formatter.parseDateTime(startTime);
		DateTime end = formatter.parseDateTime(endTime);
		int month = Months.monthsBetween(start, end).getMonths();
		// 计算天数
		Calendar bef = DateUtil.calendar(DateUtil.parse(startTime, DATE_FORMAT_YMD));
		Calendar aft = DateUtil.calendar(DateUtil.parse(endTime, DATE_FORMAT_YMD));
		int surplus = aft.get(Calendar.DATE) - bef.get(Calendar.DATE);
		return surplus <= 0 ? month : month + 1;
	}

	/**
	 * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
	 *
	 * @param nowTime
	 *            当前时间
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 */
	public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
		if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) {
			return true;
		}
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);
		Calendar begin = Calendar.getInstance();
		begin.setTime(startTime);
		Calendar end = Calendar.getInstance();
		end.setTime(endTime);
		if (date.after(begin) && date.before(end)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取传入日期的最后一天
	 *
	 * @param date
	 *            yyyy-MM
	 * @return
	 */
	public static String getPrecedMonthLastday(String dateStr) {
		Date date = strToDateMonth(dateStr);
		Instant instant = date.toInstant();
		ZoneId zone = ZoneId.systemDefault();
		LocalDateTime dateTime = LocalDateTime.ofInstant(instant, zone);
		return dateStr + "-" + dateTime.toLocalDate().lengthOfMonth();
	}

	/**
	 * 将字符串转换为yyyy-MM-dd格式的日期
	 *
	 * @param date
	 * @return 转换后的日期对象
	 */
	public static Date strToDateMonth(String date) {
		return strToFormatDate(date, "yyyy-MM");
	}

	/**
	 * 将日期字符串转化为需要格式的日期
	 *
	 * @param date
	 *            日期字符串
	 * @param format
	 *            格式字符串
	 * @return 转换后的日期对象
	 */
	public static Date strToFormatDate(String date, String format) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(date, new ParsePosition(0));
	}

	/**
	 * 获取当前时间 yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getCurrent() {
		return getCurrent(DATE_FORMAT_YMDHMS);
	}

	/**
	 * 将字符串时间转成日期时间
	 *
	 * @param nowDate
	 *            当前时间
	 * @param format
	 *            转的格式，如[yyyy-MM-dd],[yyyy年MM月dd日 HH时mm分ss秒]
	 */
	public static String getCurrent(String format) {
		return new SimpleDateFormat(format).format(new Date());
	}

	/**
	 * 获取当前年度,格式yyyy
	 */
	public static String getCurrentYear() {
		return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
	}
	
	/**
	 * 获取当前月份,格式MM
	 */
	public static String getCurrentMonth() {
		return getCurrent(DATE_FORMAT_YM).substring(4, 6);
	}

	/**
	 * 获取当前时间 yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getCurrentYmd() {
		return getCurrent(DATE_FORMAT_YMD);
	}

	/**
	 * 获取当前时间 yyyyMMddHHmmss
	 * 
	 * @return
	 */
	public static String getCurrentAll() {
		return getCurrent(DATEFORMATYMDHMS);
	}

	/**
	 * 获取上个年度,格式yyyy
	 */
	public static String getLastYear() {
		return String.valueOf(Calendar.getInstance().get(Calendar.YEAR) - 1);
	}

	/**
	 * 获取当前月的第一天
	 */
	public static String getThisMonthFirstDay() {
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(cale.getTime());
	}

	/**
	 * 获取当前月的最后一天
	 */
	public static String getThisMonthLastday() {
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.DAY_OF_MONTH, 0);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(cale.getTime());
	}

	/**
	 * 获取上月的第一天
	 */
	public static String getPrecedMonthFirstDay() {
		Calendar cale = Calendar.getInstance();
		cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, -1);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(cale.getTime());
	}

	/**
	 * 获取上月的最后一天
	 */
	public static String getPrecedMonthLastday() {
		Calendar cale = Calendar.getInstance();
		cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 0);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(cale.getTime());
	}

	/**
	 * 获取上上个月的第一天
	 */
	public static String getLastPrecedMonthFirstDay() {
		Calendar cale = Calendar.getInstance();
		cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, -2);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(cale.getTime());
	}

	/**
	 * 获取上上个月的最后一天
	 */
	public static String getLastPrecedMonthLastday() {
		Calendar cale = Calendar.getInstance();
		cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, -1);
		cale.set(Calendar.DAY_OF_MONTH, 0);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(cale.getTime());
	}

	/**
	 * 获取当前季度的起
	 */
	public static String getThisQuarterStart() {
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3);
		startCalendar.set(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(startCalendar.getTime());
	}

	/**
	 * 获取当前季度的止
	 */
	public static String getThisQuarterEnd() {
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3) * 3 + 2);
		endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		setMaxTime(endCalendar);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(endCalendar.getTime());
	}

	/**
	 * 获取上个季度的起
	 */
	public static String getLastQuarterStart() {
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
		startCalendar.set(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(startCalendar.getTime());
	}

	/**
	 * 获取上个季度的止
	 */
	public static String getLastQuarterEnd() {
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
		endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(endCalendar.getTime());
	}

	/**
	 * 获取上上个季度的起
	 */
	public static String getLastLastQuarterStart() {
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 2) * 3);
		startCalendar.set(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(startCalendar.getTime());
	}

	/**
	 * 获取上上个季度的止
	 */
	public static String getLastLastQuarterEnd() {
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 2) * 3 + 2);
		endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(endCalendar.getTime());
	}

	/**
	 * 设置最小时间
	 *
	 * @param calendar
	 */
	private static void setMinTime(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
	}

	/**
	 * 设置最大时间
	 *
	 * @param calendar
	 */
	private static void setMaxTime(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
	}

	/**
	 * 获取当年的第一天
	 */
	public static String getCurrYearFirst() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(getYearFirst(currentYear));
	}

	/**
	 * 获取当年的最后一天
	 */
	public static String getCurrYearLast() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(getYearLast(currentYear));
	}

	/**
	 * 获取去年的第一天
	 */
	public static String getLastYearFirst() {
		Calendar currCal = Calendar.getInstance();
		int year = currCal.get(Calendar.YEAR) - 1;
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(getYearFirst(year));
	}

	/**
	 * 获取去年的最后一天
	 */
	public static String getLastYearLast() {
		Calendar currCal = Calendar.getInstance();
		int year = currCal.get(Calendar.YEAR) - 1;
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(getYearLast(year));
	}

	/**
	 * 获取前年的第一天
	 */
	public static String getBeforeLastYearFirst() {
		Calendar currCal = Calendar.getInstance();
		int year = currCal.get(Calendar.YEAR) - 2;
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(getYearFirst(year));
	}

	/**
	 * 获取前年的最后一天
	 */
	public static String getBeforeLastYearLast() {
		Calendar currCal = Calendar.getInstance();
		int year = currCal.get(Calendar.YEAR) - 2;
		return new SimpleDateFormat(DATE_FORMAT_YMD).format(getYearLast(year));
	}

	/**
	 * 获取某年第一天日期
	 *
	 * @param year
	 *            年份
	 */
	public static Date getYearFirst(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}

	/**
	 * 获取某年最后一天日期
	 *
	 * @param year
	 *            年份
	 */
	public static Date getYearLast(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();
		return currYearLast;
	}

	/**
	 * 获取指定年月的第一天
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getFirstDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最小天数
		int firstDay = cal.getMinimum(Calendar.DATE);
		// 设置日历中月份的最小天数
		cal.set(Calendar.DAY_OF_MONTH, firstDay);
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取指定年月的最后一天
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getLastDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DATE);
		// 设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime());
	}

	/**
	 * 推算周期,月初
	 * @param period 周期
	 * @param taxPeriod 日期
	 */
	public static String getPastPeriodStart(String period, String taxPeriod) throws ParseException {
		SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_YMD);
		Date date = dateformat.parse(taxPeriod);
		// 周期类型: 01:按次、02:月度、03:季度、04:年度
		if ("01".equals(period)) {
			return "";
		}
		if ("02".equals(period)) {
			Date p = DateUtils.addMonths(date, -1);
			return dateformat.format(p);
		}
		if ("03".equals(period)) {
			Date p = DateUtils.addMonths(date, -3);
			return dateformat.format(p);
		}
		if ("04".equals(period)) {
			Date p = DateUtils.addMonths(date, -12);
			return dateformat.format(p);
		}
		return null;
	}

	/**
	 * 推算周期,月末
	 */
	public static String getPastPeriodEnd(String period, String taxPeriod) throws ParseException {
		SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_YMD);
		Date date = dateformat.parse(taxPeriod);
		// 周期类型: 01:按次、02:月度、03:季度、04:年度
		if ("01".equals(period)) {
			return "";
		}
		if ("02".equals(period)) {
			Date p = DateUtils.addMonths(date, -1);
			String d = dateformat.format(p);
			// 截取年数
			String yearStr = d.substring(0, 4);
			int year = Integer.parseInt(yearStr);
			String monthStr = d.substring(5, 7);
			int month = Integer.parseInt(monthStr);
			// 设置年月
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, month);
			calendar.set(Calendar.DAY_OF_MONTH, 0);
			return dateformat.format(calendar.getTime());
		}
		if ("03".equals(period)) {
			Date p = DateUtils.addMonths(date, -3);
			String d = dateformat.format(p);
			// 截取年数
			String yearStr = d.substring(0, 4);
			int year = Integer.parseInt(yearStr);
			String monthStr = d.substring(5, 7);
			int month = Integer.parseInt(monthStr);
			// 设置年月
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, month);
			calendar.set(Calendar.DAY_OF_MONTH, 0);
			return dateformat.format(calendar.getTime());
		}
		if ("04".equals(period)) {
			Date p = DateUtils.addMonths(date, -12);
			String d = dateformat.format(p);
			// 截取年数
			String yearStr = d.substring(0, 4);
			int year = Integer.parseInt(yearStr);
			String monthStr = d.substring(5, 7);
			int month = Integer.parseInt(monthStr);
			// 设置年月
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, month);
			calendar.set(Calendar.DAY_OF_MONTH, 0);
			return dateformat.format(calendar.getTime());
		}
		return null;
	}

	/**
	 * 获取某时间的当前季度的起
	 */
	public static String getThisQuarterStart(String date) {
		Date dBegin = null;
		SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_YMD);
		try {
			dBegin = dateformat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calBegin = Calendar.getInstance();
		calBegin.setTime(dBegin);
		int remainder = calBegin.get(Calendar.MONTH)  % 3;
		int month = remainder != 0 ? calBegin.get(Calendar.MONTH) - remainder: calBegin.get(Calendar.MONTH);

		calBegin.set(Calendar.MONTH, month);
		calBegin.set(Calendar.DAY_OF_MONTH, calBegin.getActualMinimum(Calendar.DAY_OF_MONTH));

		calBegin.setTime(calBegin.getTime());
		return dateformat.format(calBegin.getTime());
	}

	/**
	 * 获取时间的当前季度的止
	 */
	public static String getThisQuarterEnd(String date) {
		SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_YMD);
		Date dEnd = null;
		try {
			dEnd = dateformat.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dEnd);
		int remainder = (calendar.get(Calendar.MONTH) + 1) % 3;
		int month = remainder != 0 ? calendar.get(Calendar.MONTH) + (3 - remainder) : calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.setTime(calendar.getTime());
		return dateformat.format(calendar.getTime());
	}


	/**
	 * 获取某时间的当前半年度的起
	 */
	public static String getSemiAnnualStart(String date) {
		Date dBegin = null;
		SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_YMD);
		try {
			dBegin = dateformat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calBegin = Calendar.getInstance();
		calBegin.setTime(dBegin);
		int remainder = calBegin.get(Calendar.MONTH)  % 6;
		int month = remainder != 0 ? calBegin.get(Calendar.MONTH) - remainder: calBegin.get(Calendar.MONTH);

		calBegin.set(Calendar.MONTH, month);
		calBegin.set(Calendar.DAY_OF_MONTH, calBegin.getActualMinimum(Calendar.DAY_OF_MONTH));

		calBegin.setTime(calBegin.getTime());
		return dateformat.format(calBegin.getTime());
	}

	/**
	 * 获取时间的当前半年度的止
	 */
	public static String getSemiAnnualEnd(String date) {
		SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_YMD);
		Date dEnd = null;
		try {
			dEnd = dateformat.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dEnd);
		int remainder = (calendar.get(Calendar.MONTH) + 1) % 6;
		int month = remainder != 0 ? calendar.get(Calendar.MONTH) + (6 - remainder) : calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.setTime(calendar.getTime());
		return dateformat.format(calendar.getTime());
	}

   public static String getCurDateStr(){
		SimpleDateFormat sdf=new SimpleDateFormat(DateTimeUtil.DATE_FORMAT_YMD);
		return sdf.format(new Date());
   }

	/**
	 * 获取当前日期的前N天
	 * @param preDays
	 * @return
	 */
   public static String getPreDayStr(int preDays){
   	 SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_YMD);
     int days=-preDays;
     Calendar ca = Calendar.getInstance();
     ca.setTime(new Date());
     ca.add(Calendar.DATE, days);
     return dateformat.format(ca.getTime());
   }
}
